package com.yunlong.controller;

import com.yunlong.dto.MessageDto;
import com.yunlong.form.UserEmailForm;
import com.yunlong.form.UserPhoneForm;
import com.yunlong.service.MessageService;
import com.yunlong.util.MessageUtil;
import com.yunlong.wrapper.ResultWrapper;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author guorui
 * @date 2020-07-24 15:36
 */
@CrossOrigin
@RequestMapping("/message")
@RestController
@Slf4j
public class MessageController {
    @Resource
    private MessageService messageService;

    @ApiOperation(value = "发送短信验证码", produces = "application/json")
    @PutMapping("/shortMessage")
    public ResultWrapper getSsm(@RequestBody UserPhoneForm number) {
        //先设置为成功状态
        String successState = "OK";
        try {
            //发送信息
            MessageDto messageDto= messageService.getSsm(number.getUserPhone());
            //如果状态为ok为成功
            if (successState.equals(messageDto. getState())) {
                return ResultWrapper.success("发送成功");
            }else {
                return ResultWrapper.failure("发送失败");
            }
        } catch (Exception e) {
            return ResultWrapper.failure(e.toString ());
        }

    }
    @ApiOperation(value = "发送邮件验证码", produces = "application/json")
    @PutMapping("/email/register")
    public ResultWrapper sendEmail(@RequestBody UserEmailForm userEmailForm) {
        //先设置为成功状态
        boolean successState = true;
        try {
            //发送信息
            MessageDto messageDto = messageService.sendEmailOfRegister(userEmailForm.getUserEmail());
            //如果状态为ok为成功
            if (successState == messageDto.isOk()) {
                return ResultWrapper.success("发送成功");
            }else {
                return ResultWrapper.failure("发送失败");
            }
        } catch (Exception e) {
            return ResultWrapper.failure(e.toString ());
        }

    }
}
